﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Slf.Factories;
using Slf;

namespace SLF.Test.Factories
{
  [TestClass]
  public class NamedLoggerFactoryBaseTest
  {
    [TestMethod]
    public void GetLogger_WithName_ReturnsLoggerWithGivenName()
    {
      TestLoggerFactory target = new TestLoggerFactory();

      TestLogger logger = target.GetLogger("foo") as TestLogger;
      Assert.AreEqual("foo", logger.GetName());
    }


    [TestMethod]
    public void Requesting_A_Logger_With_Null_Uses_Default_Name()
    {
      TestLoggerFactory target = new TestLoggerFactory();

      TestLogger logger = target.GetLogger(null) as TestLogger;
      Assert.AreEqual(LoggerService.DefaultLoggerName, logger.GetName());
    }


    [TestMethod]
    public void GetLogger_WithSameName_ReturnsSameInstance()
    {
      TestLoggerFactory target = new TestLoggerFactory();

      ILogger logger = target.GetLogger("foo");
      ILogger loggerTwo = target.GetLogger("foo");

      Assert.AreSame(logger, loggerTwo);
    }

    [TestMethod]
    public void GetLogger_WithSameNameCaseInsensitive_ReturnsSameInstance()
    {
      TestLoggerFactory target = new TestLoggerFactory();

      ILogger logger = target.GetLogger("foo");
      ILogger loggerTwo = target.GetLogger("FOO");

      Assert.AreSame(logger, loggerTwo);
    }

    [TestMethod]
    public void Requesting_Loggers_By_Different_Names_Returns_Different_Instances()
    {
      TestLoggerFactory target = new TestLoggerFactory();

      ILogger logger = target.GetLogger("foo");
      ILogger loggerTwo = target.GetLogger("foo.bar");

      Assert.AreNotSame(logger, loggerTwo);
    }

    [TestMethod]
    public void GetLogger_WithDifferentNames_ReturnsLoggerWithGivenNames()
    {
      TestLoggerFactory target = new TestLoggerFactory();

      TestLogger logger = target.GetLogger("foo") as TestLogger;
      TestLogger loggerTwo = target.GetLogger("foo.bar") as TestLogger;

      Assert.AreEqual("foo", logger.GetName());
      Assert.AreEqual("foo.bar", loggerTwo.GetName());
    }

    /// <summary>
    /// A logger which has a method for exposing the name
    /// </summary>
    public class TestLogger : LoggerBase
    {
      public TestLogger(string name)
        : base(name)
      {
      }

      public string GetName()
      {
        return base.Name;
      }

      public override void Log(LogItem item)
      {
        
      }
    }

    /// <summary>
    /// A factory for the above logger
    /// </summary>
    public class TestLoggerFactory : NamedLoggerFactoryBase<TestLogger>
    {
      protected override TestLogger CreateLogger(string name)
      {
        return new TestLogger(name);
      }
    }

  }
}
